﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.MetaAttribute;
using Csla.Wpf;
using OpenExpressApp.Command;
using Csla.Core;
using System.ComponentModel;
using System.Windows;
using OpenExpressApp.Template;
using OpenExpressApp.Types;
using Csla;
using OpenExpressApp.WPF.Controls;
using System.Collections.ObjectModel;
using log4net;
using System.Collections;
using OpenExpressApp.MetaModel;
using Avalon.Windows.Controls;
using OpenExpressApp.WPF.Templete;
using OpenExpressApp.Module;
using OpenExpressApp.Module.WPF;
using System.Windows.Controls;
using Ricciolo.Controls;
using System.Reflection;
using System.Windows.Markup;
using System.Diagnostics;
using AvalonDock;
using OpenExpressApp.Module.WPF.Editors;
using Csla.OpenExpressApp;

namespace OpenExpressApp.WPF.Command
{
    /// <summary>
    /// 删除子对象
    /// </summary>
    [Command(CommandNames.DeleteChildObject, Label = "删除", ToolbarType = ToolbarType.Child, ToolTip = "删除记录")]
    public class DeleteChildObjectCommand : WPFListViewCommand
    {
        public override bool CanExecute(ListObjectView view)
        {
            if (view == null)
            {
                return false;
            }
            if (base.CanExecute(view) == false)
            {
                return false;
            }
            var bindingList = view.Data as IBindingList;
            if (bindingList == null || bindingList.AllowRemove == false)
            {
                return false;
            }
            return view.CurrentObject != null;
        }
        public override void Execute(ListObjectView view)
        {
            //获取选中对象的副本（使用副本是因为下面的操作，会改变选中的对象列表。）
            IList list = view.SelectedObjects.OfType<object>().ToArray();
            var treeControl = view.Control as MultiObjectTreeView;
            var dataList = view.Data as IBindingList;
            var item = view.CurrentObject as BusinessBase;
            int index = dataList.Count - 1;
            for (int i = list.Count - 1; i >= 0; i--)
            {
                item = list[i] as BusinessBase;
                //子对象不能直接调用这个方法。
                //item.ApplyEdit();
                if (item is ITreeNode)
                {
                    treeControl.Expand(item);
                    var children = (item as ITreeNode).GetRecurChildren();
                    //先删除细记录再删除主记录
                    for (int j = children.Count - 1; j >= 0; j--)
                    {
                        var child = children[j] as BusinessBase;
                        index = Math.Min(index, dataList.IndexOf(child));
                        dataList.Remove(child);
                        //删除树节点
                        treeControl.RemoveNode(child);
                    }
                }
                else
                {
                    //如果处于多对象树中，但不是根对象
                    if (view.IsShowingTree)
                    {
                        item.Parent.RemoveChild(item);
                        treeControl.RemoveNode(item);
                    }
                    else
                    {
                        index = dataList.IndexOf(item);
                        dataList.Remove(item);
                    }
                }
            }
            if (view.IsShowingTree)
            {
                var treeList = dataList as ITreeNodeCollection;
                if (treeList != null)
                {
                    var node = item as ITreeNode;
                    if (node != null)
                    {
                        view.CurrentObject = treeList.FindNodeAtSameLevel(node, false) ?? treeList.FindNodeAtSameLevel(node, true);
                    }
                }
            }
            else
            {
                view.CurrentObject = index >= 1 && index <= dataList.Count
                    ? dataList[index - 1] : null;
            }
        }
    }
}
